using System.Web.Mvc;
using Stimulus360.Services;
using Stimulus360.Services.Interfaces;

namespace Stimulus360.Controllers.Admin
{
    [HandleError]
    public class AdminController : Controller
    {
        /// <summary>
        ///     Instance of CRMService to be used
        /// </summary>
        private readonly ICrmSyncService _crmService;

        /// <summary>
        ///     Default constructor. Creates instance of CRMService to be used
        /// </summary>
        public AdminController()
            : this(new CrmSyncService())
        {
        }

        /// <summary>
        ///     Dependecy Injected constructor. Sets the ICRMService instance to be used
        /// </summary>
        /// <param name="crmService">
        ///     The ICRMService instance to be used.
        /// </param>
        public AdminController(ICrmSyncService crmService)
        {
            _crmService = crmService ?? new CrmSyncService();
        }

        /// <summary>
        ///     Method renders the Admin view
        /// </summary>
        /// <returns>
        ///     Admin View
        /// </returns>
        [Authorize(Users="admin")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///     Method is use to sync data from stimulus database to the CRM database
        /// </summary>
        /// <returns>
        ///     Admin view with sync. message
        /// </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Users = "admin")]
        public ActionResult SyncToCrm()
        {
            _crmService.SyncToCrm();
            return RedirectToAction("Index");
        }

        /// <summary>
        ///     Method is use to sync data from the CRM database to the stimulus database
        /// </summary>
        /// <returns>
        ///     Admin view with sync. message
        /// </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Users = "admin")]
        public ActionResult SyncFromCrm()
        {
            _crmService.SyncFromCrm();
            return RedirectToAction("Index");
        }
    }
}
